package isquirrel.beans;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import isquirrel.UserManager;
import isquirrel.recommender.NetworkRecommender;
import isquirrel.semantic.DBpedia;
import isquirrel.util.HibernateUtil;

import java.util.Iterator;
import java.util.List;

import org.junit.Test;

public class UserTest {
	
	public User createUser1() {
		User u = new User();
		u.setFirstName("Avgoustinos");
		u.setLastName("Kadis");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		return u;
	}
	
	public User createUser2() {
		User u = new User();
		u.setFirstName("George");
		u.setLastName("Mixail");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		return u;
	}
	
	public void addWikipage(User u, String wikipediaURL) {
		DBpedia dbPedia = new DBpedia();
		List<Property> wikiProperties= dbPedia.getPageInformation(wikipediaURL);
		Iterator<Property> wiki_iter = wikiProperties.iterator();
		while (wiki_iter.hasNext()){
			u.addProperty(wiki_iter.next());
		}
	}
	

	
	@Test
	public void testAddFriend() {
		HibernateUtil.useTestDB();
		
		//add test data
		HibernateUtil.beginTransaction();
	
		User u = new User();
		u.setFirstName("Avgoustinos");
		u.setLastName("Kadis");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		
		User u2 = new User();
		u2.setFirstName("George");
		u2.setLastName("Mixail");
		
		HibernateUtil.getSession().saveOrUpdate(u2);
		
		u.addFriend(u2);
		
		HibernateUtil.commit();
		
		//check the added data
		HibernateUtil.beginTransaction();
		
		User uu1 = (User) HibernateUtil.getSession().load(User.class, u.getUserID());
		User uu2 = (User) HibernateUtil.getSession().load(User.class, u2.getUserID());
		
		assert(uu1.getFriends().contains(uu2));
		
		//additional test (not really needed)
		User firstFriend = (User) uu1.getFriends().toArray()[0]; 
		assertEquals(firstFriend.getFirstName(),"George");
		assertEquals(firstFriend.getLastName(),"Mixail");
	}

	@Test
	public void testRemoveFriend() {
		
	}
	
	@Test
	public void testAddInterest() {
		HibernateUtil.useTestDB();
		
		//add test data
		HibernateUtil.beginTransaction();
		
		User u1 = createUser1();
		
		u1.addInterest(new Interest("Sports"));
		u1.addInterest(new Interest("Football"));
		
		HibernateUtil.commit();
		
		HibernateUtil.beginTransaction();
		
		User uu1 = UserManager.getUser(u1.getUserID());
		
		System.out.println(uu1.getInterests().toString());
		System.out.println(new Interest("Sports"));
		
		assertFalse(uu1.getInterests().contains(new Interest("Tennis")));
		assertTrue(uu1.getInterests().contains(new Interest("Sports")));
		assertTrue(uu1.getInterests().contains(new Interest("Football")));
		
		HibernateUtil.commit();
	}
	
	@Test
	public void testAddUrl() {
		HibernateUtil.useTestDB();
		
		//add test data
		HibernateUtil.beginTransaction();
		
		User u1 = createUser1();

		Url url1 = new Url("Phigita.net","http://www.phigita.net");
		Url url2 = new Url("UBS","http://www.ubs.co.uk"); 
		u1.addUrl(url1);
		u1.addUrl(url2);

		url1.setLiked(true);
		
		HibernateUtil.commit();
		
		HibernateUtil.beginTransaction();
		
		User uu1 = UserManager.getUser(u1.getUserID());
		
		assertTrue(uu1.getUrls().contains(new Url("zzz","http://www.phigita.net")));
		assertTrue(uu1.getUrls().contains(new Url("zzz","http://www.ubs.co.uk")));
		assertFalse(uu1.getUrls().contains(new Url("zzz","http://www.deutschebank.co.uk")));
		
		HibernateUtil.commit();
	}
	
	@Test
	public void testAddProperty() {
		HibernateUtil.useTestDB();
		
		//add test data
		HibernateUtil.beginTransaction();
		
		User u1 = createUser1();
		u1.addProperty(new Property("skos:subject","owl:Thing"));
		u1.addProperty(new Property("skos:subjects","owl:Things"));
		u1.addProperty(new Property("skos:subject","owl:Thing"));
		
		assertTrue(u1.getProperties().contains(new Property("skos:subject","owl:Thing")));
		Property p1 = u1.getProperty(new Property("skos:subject","owl:Thing"));
		assertEquals(2,p1.getFrequency());
		assertEquals(1,u1.getProperty(new Property("skos:subjects","owl:Things")).getFrequency());
		assertNull(u1.getProperty(new Property("skos:subjects","owl:Nothing")));
		
		HibernateUtil.commit();
	}
	@Test
	public void userCompare(){
		HibernateUtil.useTestDB();
		
		HibernateUtil.beginTransaction();
		
		User u1 = createUser1();
		User u2 = createUser2();
		
		u1.addProperty(new Property("skos:subject","owl:Thing"));
		u1.addProperty(new Property("skos:subjects","owl:Things"));
		u1.addProperty(new Property("skos:subject","owl:Thing"));
		u2.addProperty(new Property("skos:subject","owl:Thing"));
		u2.addProperty(new Property("skos:subjects","owl:Things"));
		u2.addProperty(new Property("skos:subject","owl:Thing"));
		double ratio = (double) u1.compare(u2);
		System.out.println("The compare ratio for users 1 & 2 is "+ratio);
		assertTrue(ratio>=0&&ratio<=1);
		HibernateUtil.commit();
		
	}

	@Test
	public void getKeyword(){
		HibernateUtil.useTestDB();
		
		//add test data
		HibernateUtil.beginTransaction();
		System.out.println("******************");
			User u = createUser1();
			String wikipediaURL = "http://en.wikipedia.org/wiki/Michael_moore";
			String wikipediaURL2 = "http://en.wikipedia.org/wiki/George_W._Bush";
			String wikipediaURL3 = "http://en.wikipedia.org/wiki/Bill_Clinton";
			String wikipediaURL4 = "http://en.wikipedia.org/wiki/John_F._Kennedy";
			String wikipediaURL5 = "http://en.wikipedia.org/wiki/William_McKinley";
			
			
			System.out.println("Adding : "+wikipediaURL);
			addWikipage(u, wikipediaURL);
			System.out.println("Adding : "+wikipediaURL2);
			addWikipage(u, wikipediaURL2);
			System.out.println("Adding : "+wikipediaURL3);
			addWikipage(u, wikipediaURL3);
			System.out.println("Adding : "+wikipediaURL4);
			addWikipage(u, wikipediaURL4);
			System.out.println("Adding : "+wikipediaURL5);
			addWikipage(u, wikipediaURL5);
			System.out.println(u.getKeyword());
			System.out.println(u.getKeyword());
			System.out.println(u.getKeyword());
			System.out.println(u.getKeyword());
			System.out.println(u.getKeyword());
			HibernateUtil.commit();
	}
	
	@Test
	public void testGetFriendsUrls() {
		HibernateUtil.useTestDB();
		HibernateUtil.beginTransaction();

		String wikipediaURL2 = "http://en.wikipedia.org/wiki/George_W._Bush";
		String wikipediaURL3 = "http://en.wikipedia.org/wiki/Bill_Clinton";
		
		User u1 = createUser1();
		User u2 = createUser2();
		
		addWikipage(u1, wikipediaURL2);
		System.out.println("**** added bush  to user 1*****");
		
		addWikipage(u2, wikipediaURL3);
		System.out.println("**** added clinton to user 2*****");
		
		u1.addFriend(u2);
		
		
		List<FriendLike> friendsUrls = NetworkRecommender.getFriendsUrls(u1, 10);
		if (friendsUrls != null) {
			for (FriendLike like : friendsUrls) {
				if (like != null) 
					System.out.println(like.url().getTitle() + "\n");
			}
		}
		else {
			System.out.println("No friends, or no likes yet");
		}
		HibernateUtil.commit();
	}
	
}
